/**
 * Copyright © 2013 南京思创信息技术有限公司. *
 * 版权所有.
 *
 * 类名　　    : WebWindow
 * 功能概要    : 网页内容显示页面. * 做成日期    : 2013/07/04 wangxw
 */
package com.sevenit.nj.yrmtfish.online;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextPaint;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.sevenit.nj.yrmtfish.R;
import com.sevenit.nj.yrmtfish.common.AppManager;
import com.sevenit.nj.yrmtfish.common.Utils;

/**
 * 网页内容显示页面的类
 * @author wangxw
 * @version 1.0
 */
@SuppressLint("HandlerLeak") 
public class WebWindow extends Activity {
	
	//进入本画面的初始URL
	String _strUrl = "";
	
	//提示对话框.
	ProgressDialog _proDialog;
		
	//webView.
	WebView _wView;
	
	/**
	 * onCreate
	 */
	public void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		
		//自定义标题栏
		requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
		//设置画面布局
		this.setContentView(R.layout.web_window);
		getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.title);
		
		//添加该Activity到AppManager
		AppManager.addActivity(this);

		//获取前画面传过来的参数.
		getPara();
		
		//设置画面控件
		setControl();
	}
	
	/**
	 * 获取前画面传过来的参数.
	 */
	private void getPara() {
		
		//获取传入的参数.
		Intent intentT = getIntent();
		_strUrl = intentT.getStringExtra("url");
		
		if(Utils.isNull(_strUrl)){
			Toast.makeText(WebWindow.this, R.string.msg_url_empty, Toast.LENGTH_LONG).show();
			AppManager.closePage(WebWindow.class.getName());
		}
	}
	
	/**
	 * 设置画面控件
	 */
	private void setControl() {
		
		//设置标题文字
		TextView txtTitle = (TextView)findViewById(R.id.txt_title);
		txtTitle.setText("");
		//设置为粗体
		TextPaint paint = txtTitle.getPaint();
		paint.setFakeBoldText(false);
		txtTitle.setTextSize(14);
		txtTitle.setGravity(Gravity.RIGHT);
		txtTitle.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
		
		//设置[返回]按钮按下效果
		ImageButton button = (ImageButton)findViewById(R.id.btn_back);
		button.setOnTouchListener(new ImageButton.OnTouchListener() {
			@Override
			public boolean onTouch(View arg0, MotionEvent arg1) {
				if (arg1.getAction() == MotionEvent.ACTION_DOWN) {
					arg0.setBackgroundResource(R.drawable.btn_back_p);
				} else if (arg1.getAction() == MotionEvent.ACTION_UP) {
					arg0.setBackgroundResource(R.drawable.btn_back);
				}
				return false;
			}
        });	
		//点击[返回]按钮的响应.
		button.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				_wView.destroy();
	        	AppManager.closePage(WebWindow.class.getName());
			}
		});
		
		//webView.
		_proDialog = ProgressDialog.show(WebWindow.this, "提示", "正在访问，请稍候...", true, true);
		_wView = (WebView) this.findViewById(R.id.web_detail);
		_wView.loadUrl(_strUrl);
		// 如果页面中链接，如果希望点击链接继续在当前browser中响应,
		// 而不是新开Android的系统browser中响应该链接，必须覆盖webview的WebViewClient对象.
		_wView.setWebViewClient(new WebViewClient() {
			
			@Override
            public void onPageFinished(WebView view, String url) {
                    // TODO Auto-generated method stub
                    super.onPageFinished(view, url);
                    //页面下载完毕,却不代表页面渲染完毕显示出来
                    //WebChromeClient中progress==100时也是一样
                    //if (_wView.getContentHeight() != 0) {
                	//这个时候网页才显示
                	//if (_proDialog != null) {
    				//	_proDialog.dismiss();
    				//}
                   // }
            }
			
			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url){
				//  重写此方法表明点击网页里面的链接还是在当前的webview里跳转，不跳到浏览器那边.
				if(!Utils.isNull(url)){
					if(_proDialog == null){
						_proDialog = ProgressDialog.show(WebWindow.this, "提示", "正在访问，请稍候...", true, true);
					}
					_wView.loadUrl(url);
				}
				return true;
			}
		});
		_wView.setTag(txtTitle);
		_wView.setWebChromeClient(new WebChromeClient() {
			
			@Override
			public void onProgressChanged(WebView view, int progress) {
				
				TextView txtTitle = (TextView)view.getTag();
				if (progress > 20) {
					if (_proDialog != null) {
						_proDialog.dismiss();
						_proDialog= null;
					}
					txtTitle.setText(progress + "%");
				}
				if (progress == 100){
					txtTitle.setText("");
				}
			}
		});
	}

	/**
	 * 监听手机硬件上的返回等按钮
	 */
	@Override 
    public boolean onKeyDown(int keyCode, KeyEvent event) { 
        if (keyCode == KeyEvent.KEYCODE_BACK &&	event.getRepeatCount() == 0) {
        	_wView.destroy();
        	AppManager.closePage(WebWindow.class.getName());
            return false; 
        }
        return false; 
    }
}
